#pragma once
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <string>
#include <functional>
#include"log.hpp"
#include<string>

using namespace std;


void serverIO(int sock)
{
    char buffer[1024];
    while (true)
    {
        ssize_t n = read(sock, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            // 目前把读到的数据当成字符串，截至目前
            buffer[n] = 0;
            cout << "recv message: " << buffer << endl;

            string outbuffer = buffer;
            outbuffer += "server[echo]";

            write(sock, outbuffer.c_str(), outbuffer.size()); // 返回值问题，多路转接
        }
        else if (n == 0)
        {
            // 代表client退出
            logMessage(NOMAL, "client quit ,me too");
            break;
        }
    }
    close(sock);
}

class Task
{
using func_t = std::function<void(int)>;

public:
    Task()
    {
    }
    Task(int sock, func_t func)
        : _sock(sock), _callblack(func)
    {
    }
    void operator()()
    {
        _callblack(_sock);
    }

    ~Task()
    {
    }

private:
    int _sock;
    func_t _callblack;
};
